package com.blb.trans.service.impl;

import com.blb.trans.entity.Student;
import com.blb.trans.mapper.StudentMapper;
import com.blb.trans.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateRequired(int id, int age){
        Student student = new Student();
        student.setStuId(id);
        student.setStuAge(age);
        studentMapper.update(student);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateRequiredException(int id, int age){
        Student student = new Student();
        student.setStuId(id);
        student.setStuAge(age);
        studentMapper.update(student);
        throw new RuntimeException("Error");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateRequiresNew(int id, int age){
        Student student = new Student();
        student.setStuId(id);
        student.setStuAge(age);
        studentMapper.update(student);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateRequiresNewException(int id, int age){
        Student student = new Student();
        student.setStuId(id);
        student.setStuAge(age);
        studentMapper.update(student);
        throw new RuntimeException("Error");
    }

    @Transactional(propagation = Propagation.NESTED)
    @Override
    public void updateNested(int id, int age) {
        Student student = new Student();
        student.setStuId(id);
        student.setStuAge(age);
        studentMapper.update(student);
    }

    @Transactional(propagation = Propagation.NESTED)
    @Override
    public void updateNestedException(int id, int age) {
        Student student = new Student();
        student.setStuId(id);
        student.setStuAge(age);
        studentMapper.update(student);
        throw new RuntimeException("Error");
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void updateSupports(int id, int age) {
        Student student = new Student();
        student.setStuId(id);
        student.setStuAge(age);
        studentMapper.update(student);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void updateSupportsException(int id, int age) {
        Student student = new Student();
        student.setStuId(id);
        student.setStuAge(age);
        studentMapper.update(student);
        throw new RuntimeException("Error");
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public void updateNotSupportsException(int id, int age) {
        Student student = new Student();
        student.setStuId(id);
        student.setStuAge(age);
        studentMapper.update(student);
        throw new RuntimeException("Error");
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public void updateNotSupports(int id, int age) {
        Student student = new Student();
        student.setStuId(id);
        student.setStuAge(age);
        studentMapper.update(student);
    }

//    @Transactional(propagation = Propagation.NEVER)
    @Override
    public void updateNever(int id, int age) {
        Student student = new Student();
        student.setStuId(id);
        student.setStuAge(age);
        studentMapper.update(student);
    }

    @Transactional(propagation = Propagation.MANDATORY)
    @Override
    public void updateMandatory(int id, int age) {
        Student student = new Student();
        student.setStuId(id);
        student.setStuAge(age);
        studentMapper.update(student);
    }
}
